#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
	int index;
	char name[25];
	int year, month, day;
} Student;
Student x[110];
int cmp(const void *a, const void *b)
{
	int aindex = ((Student *)a)->index;
	int ayear = ((Student *)a)->year,
		amonth = ((Student *)a)->month,
		aday = ((Student *)a)->day;
	int bindex = ((Student *)b)->index,
		byear = ((Student *)b)->year,
		bmonth = ((Student *)b)->month,
		bday = ((Student *)b)->day;
	if (ayear < byear) // 年份小，年龄大
	{
		return -1;
	}
	else if (ayear > byear)
	{
		return 1;
	}
	else
	{
		if (amonth < bmonth) // 月份小，年龄大
		{
			return -1;
		}
		else if (amonth > bmonth)
		{
			return 1;
		}
		else
		{
			if (aday < bday)
			{
				return -1;
			}
			else if (aday > bday)
			{
				return 1;
			}
			else
			{
				if (aindex < bindex)
				{
					return 1;
				}
				else
				{
					return -1;
				}
			}
		}
	}
}
int main()
{
	int n, i;
	scanf("%d", &n);
	for (i = 1; i <= n; ++i)
	{
		scanf("%s", x[i].name);
		scanf("%d%d%d", &x[i].year, &x[i].month, &x[i].day);
		x[i].index = i;
	}
	qsort(x + 1, n, sizeof(Student), cmp);
	for (i = 1; i <= n; ++i)
	{
		printf("%s\n", x[i].name);
	}
	return 0;
}
